/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 27/10/2004
 *
 */

package au.com.lastweekend.jim.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

import au.com.lastweekend.jim.util.TriState;

/**
 * Implements a keyword hierarchy
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: Keyword.java,v 1.6 2006/04/16 09:16:27 ggardner Exp $
 */
public class Keyword implements MutableTreeNode {

    private Keyword parent = null;
    private String name;
    private Map<String, Keyword> children = new HashMap<String, Keyword>();
    private List<Keyword> childrenList = new ArrayList<Keyword>();
    private int indexInParent = -1;
    private boolean isSearchSelected = false;
    private TriState selectedState = TriState.NOT_SELECTED;
    private boolean keywordsEnabled = false;

    /**
     * A new unattached child keyword with no name.
     */
    public Keyword() {

        this((String) null, false);

    }

    /**
     * A new unattached child keyword with name
     * 
     * @param name
     */
    public Keyword(String name) {

        this(name, false);
    }

    public Keyword(String name, boolean isRoot) {

        this.name = name;
        if (isRoot) {
            indexInParent = 0;
        }

    }

    public String getName() {

        return name;
    }

    public Keyword getParent() {

        return parent;
    }

    public Map<String, Keyword> getChildren() {

        return children;
    }

    public boolean hasChild(String name) {

        return children.containsKey(name);
    }

    public Keyword getChild(String name) {

        return children.get(name);
    }

    @Override
    public String toString() {

        return name;
    }

    public Keyword getChildAt(int index) {

        return childrenList.get(index);
    }

    public int getIndex() {

        return indexInParent;

    }

    public boolean isRoot() {

        return parent == null && indexInParent == 0;
    }

    /**
     * @param newName
     */
    public void setName(String newName) {

        if (parent != null) {
            parent.children.remove(name);
            parent.children.put(newName, this);
        }

        name = newName;

    }

    /**
     * Remove. Extracts a subtree from the keyword hierarchy
     */
    public void remove() {

        if (isRoot()) {
            // Doesn't really make sense to remove root, but doesn't hurt
            // either.
            return;
        }

        parent.remove(this);
        setParent(null);
    }

    public void insert(MutableTreeNode childNode, int index) {

        Keyword child = (Keyword) childNode;

        if (children.containsKey(child.name)) {
            throw new IllegalArgumentException("Can't insert a node that already exists");
        }

        child.setParent(this);
        childrenList.add(index, child);
        children.put(child.name, child);
        reIndex(index);

    }

    public void remove(int index) {

        Keyword node = childrenList.get(index);
        childrenList.remove(index);
        reIndex(index);
        children.remove(node.name);

    }

    private void reIndex(int fromIndex) {

        // reindex our children if we've removed or inserted something
        for (int i = fromIndex; i < childrenList.size(); i++) {
            Keyword child = childrenList.get(i);
            child.indexInParent = i;
        }
    }

    public void remove(MutableTreeNode node) {

        Keyword keyword = (Keyword) node;
        remove(keyword.indexInParent);

    }

    public void removeFromParent() {

        parent.remove(this);

    }

    /**
     * We only set the parent here.. we do not re-index the parent or remap. should really only be called by the insert method, or
     * to create temporary nodes
     */
    public void setParent(MutableTreeNode newParent) {

        parent = (Keyword) newParent;

    }

    public void setUserObject(Object object) {

        if (object instanceof String) {
            setName((String) object);
        }

    }

    public Enumeration<Keyword> children() {

        return Collections.enumeration(childrenList);
    }

    public boolean getAllowsChildren() {

        return true;
    }

    public int getChildCount() {

        return children.size();
    }

    public int getIndex(TreeNode node) {

        return childrenList.indexOf(node);
    }

    public boolean isLeaf() {

        return children.size() == 0;
    }

    public boolean isNew() {

        return name == null && !isRoot();
    }

    @Override
    public int hashCode() {

        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((parent == null) ? 0 : parent.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {

        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Keyword other = (Keyword) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (parent == null) {
            if (other.parent != null)
                return false;
        } else if (!parent.equals(other.parent))
            return false;
        return true;
    }

    /**
     * @return true if selected for searching
     */
    public boolean isSearchSelected() {

        return isSearchSelected;
    }

    /**
     * @param b
     */
    public void setSearchSelected(boolean b) {

        isSearchSelected = b;

    }

    /**
     * @return
     */
    public TriState getSelectedState() {

        return selectedState;
    }

    public void setSelectedState(TriState state) {

        this.selectedState = state;
    }

    /**
     * @param keywordsEnabled
     */
    public void setKeywordsEnabled(boolean keywordsEnabled) {

        this.keywordsEnabled = keywordsEnabled;

    }

    public boolean areKeywordsEnabled() {

        return keywordsEnabled;
    }
}